<template>
  <div class="liteflow-editor-side-bar">
    <div class="side-bar-header">
      <h3>物料区</h3>
    </div>
    <div class="side-bar-content">
      <div class="material-group">
        <h4>逻辑组件</h4>
        <div class="material-list">
          <div 
            v-for="item in logicComponents" 
            :key="item.type"
            class="material-item"
            draggable="true"
            @dragstart="handleDragStart($event, item)"
          >
            <div class="material-icon">
              <img :src="item.icon" :alt="item.name" />
            </div>
            <div class="material-name">{{ item.name }}</div>
          </div>
        </div>
      </div>
      
      <div class="material-group">
        <h4>逻辑编排</h4>
        <div class="material-list">
          <div 
            v-for="item in logicOrchestration" 
            :key="item.type"
            class="material-item"
            draggable="true"
            @dragstart="handleDragStart($event, item)"
          >
            <div class="material-icon">
              <img :src="item.icon" :alt="item.name" />
            </div>
            <div class="material-name">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Graph } from '@antv/x6';
import { NodeTypeEnum, ConditionTypeEnum } from '../../constant';

export default {
  name: 'SideBar',
  props: {
    flowGraph: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      logicComponents: [
        { type: NodeTypeEnum.COMMON, name: '普通组件', icon: require('../../assets/common-icon.svg') },
        { type: NodeTypeEnum.BOOLEAN, name: '布尔组件', icon: require('../../assets/and-icon.svg') },
        { type: NodeTypeEnum.SCRIPT, name: '脚本组件', icon: require('../../assets/script-icon.svg') },
        { type: NodeTypeEnum.VIRTUAL, name: '虚节点', icon: require('../../assets/virtual-icon.svg') }
      ],
      logicOrchestration: [
        { type: ConditionTypeEnum.THEN, name: '串行编排', icon: require('../../assets/then-icon.svg') },
        { type: ConditionTypeEnum.WHEN, name: '并行编排', icon: require('../../assets/when-icon.svg') },
        { type: ConditionTypeEnum.SWITCH, name: '选择编排', icon: require('../../assets/switch-icon.svg') },
        { type: ConditionTypeEnum.IF, name: '条件编排', icon: require('../../assets/if-icon.svg') },
        { type: ConditionTypeEnum.FOR, name: '循环编排', icon: require('../../assets/for-icon.svg') },
        { type: ConditionTypeEnum.WHILE, name: '循环条件', icon: require('../../assets/while-icon.svg') }
      ]
    };
  },
  mounted() {
    this.initDropZone();
  },
  methods: {
    handleDragStart(event, item) {
      event.dataTransfer.setData('application/json', JSON.stringify(item));
    },
    initDropZone() {
      // 初始化拖拽区域
      this.$nextTick(() => {
        const graphContainer = this.flowGraph.container;
        if (graphContainer) {
          graphContainer.addEventListener('dragover', this.handleDragOver);
          graphContainer.addEventListener('drop', this.handleDrop);
        }
      });
    },
    handleDragOver(event) {
      event.preventDefault();
    },
    handleDrop(event) {
      event.preventDefault();
      const data = event.dataTransfer.getData('application/json');
      if (data) {
        const item = JSON.parse(data);
        const position = this.flowGraph.clientToLocal(event.clientX, event.clientY);
        this.createNode(item, position);
      }
    },
    createNode(item, position) {
      // 创建节点的逻辑
      console.log('Creating node:', item, 'at position:', position);
      // 这里需要根据具体的业务逻辑来实现节点创建
    }
  }
};
</script>

<style lang="less" scoped>
.liteflow-editor-side-bar {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #fff;
}

.side-bar-header {
  padding: 16px;
  border-bottom: 1px solid #d9d9d9;
  
  h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
    color: #333;
  }
}

.side-bar-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

.material-group {
  margin-bottom: 24px;
  
  h4 {
    margin: 0 0 12px 0;
    font-size: 14px;
    font-weight: 500;
    color: #666;
  }
}

.material-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.material-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 8px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background: #fff;
  cursor: grab;
  transition: all 0.2s;
  
  &:hover {
    border-color: #1890ff;
    box-shadow: 0 2px 8px rgba(24, 144, 255, 0.15);
  }
  
  &:active {
    cursor: grabbing;
  }
}

.material-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 4px;
  
  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
}

.material-name {
  font-size: 12px;
  color: #333;
  text-align: center;
  line-height: 1.2;
}
</style> 